/**
 * @param {string} s
 * @return {string}
 */
var decodeString = function (s) {
  //数字栈 字符串栈
  let numStack = []
  let strStack = []
  // 当前层状态：当前层字符串 当前层数字
  let currentStr = ''
  let currentNum = 0

  for (let char of s) {
    if (char >= '0' && char <= '9') {
      currentNum = currentNum * 10 + parseInt(char)
    } else if (char === '[') {
      //上一层状态入栈
      numStack.push(currentNum)
      strStack.push(currentStr)
      //当前层状态恢复初始
      currentStr = ''
      currentNum = 0
    } else if (char === ']') {
      //上一层状态出栈
      let preStr = strStack.pop()
      let repeatTime = numStack.pop()
      //更新当前层的字符串：上一层字符串 + 当前层字符串*上一层数字
      currentStr = preStr + currentStr.repeat(repeatTime)
    } else {
      currentStr += char
    }
  }

  //返回current：最终的current即结果
  return currentStr
};